package com.crm.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.entity.Sourcecompare;
import com.crm.entity.Sources;
import com.crm.util.ApplicationContext;

public class SourceCompareDAO {
	protected final Log logger = LogFactory.getLog(getClass());

	public  int insert(Sourcecompare s) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
				tx = session.beginTransaction();
			session.saveOrUpdate(s);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  int insertList(List<Sourcecompare> lsc) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			for(Sourcecompare s:lsc){
				session.saveOrUpdate(s);
			}
			tx.commit();
			return 0;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  int update(Sourcecompare s) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(s);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
		if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	
	public  void deletelast(int enqid,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
				tx = session.beginTransaction();
			String qry="Delete from Sourcecompare where enqnum=:eqid and business_id=:busid";
			Query query=session.createQuery(qry);
			query.setInteger("eqid", enqid);
			query.setInteger("busid",busid);
			query.executeUpdate();
			tx.commit();
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  int delete(Sourcecompare s) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
				tx = session.beginTransaction();
			session.delete(s);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  List<Sourcecompare> findAll(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			 List<Sourcecompare> custObjs=new ArrayList<Sourcecompare>();
			 List objs=null;
			Query query=session.createQuery("From Sourcescompare where is_deleted!=1 and business_id=:busid");
			query.setInteger("busid", busid);
			objs=query.list();
			for(Object o:objs){
				custObjs.add((Sourcecompare)o);
			}
			return custObjs;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public Sourcecompare findById(int i,int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String qrystr="From Sourcescompare sd where sd.id=:sid and business_id=:busid";
			Query qry=session.createQuery(qrystr);
			qry.setInteger("sid", i);
			qry.setInteger("busid", busid);
			Object ob=qry.uniqueResult();
			Sourcecompare s=(Sourcecompare)ob;
			return s;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public Sources getByName(String sname,int busid)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String querystr="From Sources where sourcename=:name and business_id=:busid";
			Query query=session.createQuery(querystr);
			query.setString("name", sname);
			query.setInteger("busid", busid);
			Object queryresult=query.uniqueResult();
			Sources s=(Sources)queryresult;
			return s;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  void deleteSource(int sourceid,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			String qtr="Update Sources set is_deleted=1 where id=:id and business_id=:busid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("busid", busid);
			qry.setInteger("id", sourceid);	
			qry.executeUpdate();
			tx.commit();
			return ;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  Sourcecompare getPartPrice(int enqid,int partid,int supplierid,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Sourcecompare sc=new Sourcecompare();
			String qtr="from Sourcecompare where business_id=:busid and enqnum=:enqid and prtnum=:partid and supplier=:supplierid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("busid", busid);
			qry.setInteger("enqid", enqid);
			qry.setInteger("partid", partid);
			qry.setInteger("supplierid", supplierid);
			Object ob=qry.uniqueResult();
			sc=(Sourcecompare) ob;
			return sc;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  Sourcecompare findByPartIdBusid(int enqid,int partid,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Sourcecompare sc=new Sourcecompare();
			String qtr="from Sourcecompare where business_id=:busid and enqnum=:enqid and prtnum=:partid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("busid", busid);
			qry.setInteger("enqid", enqid);
			qry.setInteger("partid", partid);
			Object ob=qry.uniqueResult();
			sc=(Sourcecompare) ob;
			return sc;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}


}
